﻿/**
*处理天地图多节点联通使用的layer
*/
/**
 * @class
 * @name NMultiLayer
 * @description 复合瓦片图层类
 * @inherit NWMTSLayer
 */
NMultiLayer = NWMTSLayer.extend({
    initialize: function (name, url, options, _ProParams, _CityParams) {
        NWMTSLayer.prototype.initialize.call(this, name, url, options);
        this._proParams = _ProParams;
        this._cityParams = _CityParams;
        this.layerType = 'NMultiLayer';
        this.setUrl(url);
        this.initialLevelParams();
    },

    _isArray: function (source) {
        return '[object Array]' === Object.prototype.toString.call(source);
    },

    initialLevelParams: function () {
        var cityParams = this._cityParams,
            proParams = this._proParams;
        if (!this._isArray(cityParams) || !this._isArray(proParams)) return;

        var tmpRes, maxX, minX, maxY, minY, tileWidth,
	        tmpBounds, tmpOrigin, tmpServerRes;
        for (var f = 0; f < cityParams.length; f++) {
            tmpBounds = cityParams[f].maxExtent,
			tmpOrigin = cityParams[f].tileOrigin,
			tmpServerRes = cityParams[f].serverResolutions;

            this._cityParams[f].tileBounds = {};
            for (var key in tmpServerRes) {
                if (tmpServerRes.hasOwnProperty(key)) {
                    tmpRes = tmpServerRes[key];
                    tileWidth = cityParams[f].tileSize.x * tmpRes;
                    minX = Math.round((tmpBounds.minX - tmpOrigin.x) / tileWidth);
                    maxX = Math.round((tmpBounds.maxX - tmpOrigin.x) / tileWidth);
                    minY = Math.floor((tmpOrigin.y - tmpBounds.minY) / tileWidth);
                    maxY = Math.floor((tmpOrigin.y - tmpBounds.maxY) / tileWidth);
                    this._cityParams[f].tileBounds[key] = new NBounds(minX, minY, maxX, maxY);
                }
            }
        }

        for (var f = 0; f < proParams.length; f++) {
            tmpBounds = proParams[f].maxExtent,
	        tmpOrigin = proParams[f].tileOrigin,
	        tmpServerRes = proParams[f].serverResolutions;

            this._proParams[f].tileBounds = {};
            for (var key in tmpServerRes) {
                if (tmpServerRes.hasOwnProperty(key)) {
                    tmpRes = tmpServerRes[key];
                    tileWidth = proParams[f].tileSize.x * tmpRes;
                    minX = Math.round((tmpBounds.minX - tmpOrigin.x) / tileWidth);
                    maxX = Math.round((tmpBounds.maxX - tmpOrigin.x) / tileWidth);
                    minY = Math.floor((tmpOrigin.y - tmpBounds.minY) / tileWidth);
                    maxY = Math.floor((tmpOrigin.y - tmpBounds.maxY) / tileWidth);
                    this._proParams[f].tileBounds[key] = new NBounds(minX, minY, maxX, maxY);
                }
            }
        }
    },
    _getTileUrl: function (z, y, x) {
        var cityParams = this._cityParams,
            proParams = this._proParams;
        var tmpParams, bModTag = false;
        var resultUrl = this.url;
        var params = {
            SERVICE: 'WMTS',
            REQUEST: 'GetTile',
            VERSION: this.version,
            LAYER: this.layer,
            STYLE: this.style,
            TILEMATRIXSET: this.tileMatrixSet,
            TILEMATRIX: z,
            TILEROW: y,
            TILECOL: x,
            FORMAT: this.format
        };

        if (z >= 18 && z <= 20) {
            var tmpz = parseInt(z);
            for (var f = 0; f < cityParams.length; f++) {
                if (cityParams[f].tileBounds.hasOwnProperty(tmpz)) {
                    var tmpBounds = cityParams[f].tileBounds[tmpz];
                    if (tmpBounds.contains(new NXY(x, y))) {
                        bModTag = true;
                        resultUrl = cityParams[f].url;
                        params.TILEMATRIX = tmpz;
                        params.VERSION = cityParams[f].version;
                        params.LAYER = cityParams[f].layer;
                        params.STYLE = cityParams[f].style;
                        params.TILEMATRIXSET = cityParams[f].tileMatrixSet;
                        params.FORMAT = cityParams[f].format;
                    }
                }
            }
        }
        else if (z >= 15 && z <= 17) {
            var tmpz = z;
            for (var f = 0; f < proParams.length; f++) {
                if (proParams[f].tileBounds.hasOwnProperty(tmpz)) {
                    var tmpBounds = proParams[f].tileBounds[tmpz];
                    if (tmpBounds.contains(new NXY(x, y))) {
                        bModTag = true;
                        resultUrl = proParams[f].url;
                        params.TILEMATRIX = tmpz;
                        params.VERSION = proParams[f].version;
                        params.LAYER = proParams[f].layer;
                        params.STYLE = proParams[f].style;
                        params.TILEMATRIXSET = proParams[f].tileMatrixSet;
                        params.FORMAT = proParams[f].format;
                    }
                }
            }
        }
        //if (z >= 12) {
        //    var tmpz = z;
        //    for (var f = 0; f < proParams.length; f++) {
        //        if (proParams[f].tileBounds.hasOwnProperty(tmpz)) {
        //            var tmpBounds = proParams[f].tileBounds[tmpz];
        //            if (tmpBounds.contains(new NXY(x, y))) {
        //                bModTag = true;
        //                resultUrl = proParams[f].url;
        //                params.TILEMATRIX = tmpz - 10;
        //                params.VERSION = proParams[f].version;
        //                params.LAYER = proParams[f].layer;
        //                params.STYLE = proParams[f].style;
        //                params.TILEMATRIXSET = proParams[f].tileMatrixSet;
        //                params.FORMAT = proParams[f].format;
        //            }
        //            else {
        //                return;
        //            }
        //        }
        //    }
        //}

        if (!resultUrl) return null;

        if (this.getServiceMode() == 'KVP') {
            if (!this._wmtsUrlParams && (resultUrl.charAt(resultUrl.length - 1) != '?')) resultUrl += '?';

            for (var key in params) {
                if (params.hasOwnProperty(key)) {
                    if ((this._wmtsUrlParams && this._wmtsUrlParams.hasOwnProperty(key)) || params[key] == null)
                        continue;
                    else {
                        if (resultUrl.charAt(resultUrl.length - 1) != '?' && resultUrl.charAt(resultUrl.length - 1) != '&')
                            resultUrl += '&';
                        resultUrl += key + '=' + params[key];
                    }
                }
            }
        }
        else {
            if (resultUrl.charAt(resultUrl.length - 1) != '/')
                resultUrl += '/';
            resultUrl += params.layer + '/' + params.style + '/' + params.tileMatrixSet + '/' + params.TILEMATRIX + '/' + y + '/' + x + '.' + params.getFormat(null, false);
        }

        return encodeURI(resultUrl);
    }
});

var layerP = NLayer.prototype;
layerP._update = function (a) {
    a = a || {};
    var b = a.bounds || this._map.getBounds();
    this.maxExtent.intersects(b) && this._moveTo(b);
    //this.getVisible() && this.maxExtent.intersects(b) && this._moveTo(b);
    //if(this.isBasicLayer){this._moveTo(b);}
};
/*解决map.zoomToExtent方法异常
引起二三维切换时，比例尺比正常级别始终大1级的问题，屏蔽。 fcy20131215
layerP._getZoomByRes = function(a) {
if (!a) {
return 0;
}
this.resolutions || this._initResolutions();
for (var i = 0, arr = this.resolutions, len = arr.length; i < len; i++) {
if (arr[i] < a) {
break;
}
}
var zoom = Math.max(0, i - 1);
return zoom;
};*/
layerP._checkInRange = function () {
    var a = !1;
    if (this._map) {
        var b = this._map.getResolution();
        if (this.zoomResFixed) {
            a = !1;
            var c = 0,
            d = this.resolutions.length;
            for (; c < d; c++)
                if (parseFloat(this.resolutions[c]) == b) {
                    if (this.InvisibleResAry) {
                        var f = 0,
                        ff = this.InvisibleResAry.length;
                        for (; f < ff; f++)
                            if (parseFloat(this.InvisibleResAry[f]) == b) {
                                a = !1;
                                return a;
                            }
                    }
                    a = !0;
                    break
                }
        } else a = !0;
        a && ((this.minRes === undefined || this.minRes === null || b >= this.minRes) && (this.maxRes === undefined || this.maxRes === null || b <= this.maxRes) ? a = !0 : a = !1);
        if (a) {
            var e = this._map.getExtent();
            this.maxExtent && e && !e.intersects(this.maxExtent) && (a = !1)
        }
    }
    return a;
};
